<?php

require_once ('BaseModel.php');
require_once ('DBUtils.php');
class PersistentLogin extends BaseModel {
	
	protected $userID;
	
	protected $_name = 'persistent_login';
	
	protected $_primary = array('loginID');
	
	/**
	 * 
	 * Enter description here ...
	 * @param unknown_type $userID the username
	 * @param unknown_type $id an array, consists of loginSerieID, or NULL 
	 * @param unknown_type $needQuery
	 */
	public function __construct($userID, $id = null, $needQuery = true) {
		$this->userID = $userID;
		parent::__construct ( $id, $needQuery );
	}
	
	/**
	 * Create new persistent login information
	 * (non-PHPdoc)
	 * @see application/models/BaseModel::createDefaultData()
	 */
	public function createDefaultData() {
		$data = array ();
		$data ['userID'] = $this->userID;
		$data ['loginSerieID'] = Utils::createPL_UserSerieID ();
		$data ['loginSessionID'] = Utils::createPL_LoginTokenID ();
		return $data;
	}
	
	public function abstractLoad($id) {
		$loginSerieID = $id ['loginSerieID'];
		$stmt = $this->select ()
			->where ( 'userID = :_userID' )
			->where ( 'loginSerieID = :_loginSerieID' )
			->bind ( array (':_userID' => $this->userID, ':_loginSerieID' => $loginSerieID ) );
		$rs = $this->fetchRow ($stmt);
		if ($rs != null) {
			$this->_data = $rs->toArray ();
		} else {
			$this->_data = array ();
		}
	}
	
	public function getUserID()	{
		return $this->userID;
	}
	
	public function getLoginSerieID()	{
		return $this->_data['loginSerieID'];
	}
	
	public function getLoginSessionID()	{
		return $this->_data['loginSessionID'];
	}
	
	public function setLoginSessionID($sessionID)	{
		$this->_data['loginSessionID'] = $sessionID;
	}
	
	public function isExist()	{
		if (count($this->getData()) > 0)
			return true;
		return false;
	}
	
	public function isSessionIDValid($loginSessionID)	{
		if ($this->getLoginSessionID() === $loginSessionID)	{
			return true;
		}
		return false;
	}
	
	public static function _createCookies($cookieExpire, $userID='', $loginSerieID='', $loginSessionID='')	{
		$cookieJar = new Zend_Http_CookieJar();
		
		$userIdCookie = new Zend_Http_Cookie('userID', $userID, GlobalConfig::COOKIE_DOMAIN, $cookieExpire);
		$loginSerieIdCookie = new Zend_Http_Cookie('loginSerieID', $loginSerieID, GlobalConfig::COOKIE_DOMAIN, $cookieExpire);
		$loginSessionIdCookie = new Zend_Http_Cookie('loginSessionID', $loginSessionID, GlobalConfig::COOKIE_DOMAIN, $cookieExpire);
		
		$cookieJar->addCookie($userIdCookie);
		$cookieJar->addCookie($loginSerieIdCookie);
		$cookieJar->addCookie($loginSessionIdCookie);
		
		return $cookieJar;
	}
	
	/**
	 * 
	 * Create cookies used for Persistent Login
	 */
	public function createCookies()	{
		//issue user some cookies for persistent login
		$cookieExpire = time() + GlobalConfig::$plCookieExpireTime;
		return self::_createCookies($cookieExpire, $this->userID, $this->getLoginSerieID(), $this->getLoginSessionID());
	}
	
	public static function createExpiredCookies()	{
		$cookieExpire = time() - 3600;
		return self::_createCookies($cookieExpire);
	}
	
	public static function fromCookies(Zend_Controller_Request_Http $request)	{
		$userIdCookie = $request->getCookie("userID");
		$loginSerieIdCookie = $request->getCookie("loginSerieID");
		
		if ($userIdCookie == null || $loginSerieIdCookie == null)	{
			return null;
		}
		
		$pl = new PersistentLogin($userIdCookie, array('loginSerieID'=>$loginSerieIdCookie));
		if (!$pl->isExist())	{
			return null;
		}
		return $pl;
	}
}

?>